# -*- coding: utf-8 -*-
import scrapy
import urlparse
import re
from ..items import JobItem
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


class JobSpider(scrapy.Spider):
    name = 'job'
    allowed_domains = ['51job.com']
    start_urls = ['http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=020000&keyword=python&keywordtype=2&lang=c&stype=2&postchannel=0000&fromType=1&confirmdate=9']

    def parse(self, response):
        yield scrapy.Request(
            url=response.url,
            callback=self.parse_list,
            dont_filter=True,
        )
        next_page = response.xpath("//div[contains(@class,'p_in')]/ul/li[last()]/a/@href").extract_first("")
        if next_page:
            next_page = urlparse.urljoin(response.url, next_page)
            yield scrapy.Request(
                url=next_page,
                callback=self.parse,
                dont_filter=True,
            )

    def parse_list(self, response):
        job_list = response.xpath("//div[@class='dw_table']/div[@class='el']")
        for job in job_list:
            job_name = job.xpath("p/span/a/@title").extract_first("")
            job_company = job.xpath("span[1]/a/@title").extract_first("")
            job_money = job.xpath("span[@class='t4']/text()").extract_first("").strip()
            money_dict = self.parse_money(job_money)
            min_money = money_dict['min_money']
            max_money = money_dict['max_money']
            job_money = min_money
            job_place = job.xpath("span[@class='t3']/text()").extract_first("").strip()
            print job_name, job_company, job_money, job_place
            item = JobItem()
            item['job_name'] = job_name
            item['job_company'] = job_company
            item['job_money'] = job_money
            item['job_place'] = job_place
            yield item

    def parse_money(self, money):
        if u"万" in money and u"月" in money:
            # [a-z]
            pattern = re.compile(u"[-万月/]")
            result = pattern.split(money)

            if len(result) >= 2:
                min_money = float(result[0]) * 10000
                max_money = float(result[1]) * 10000
            else:
                min_money = float(result[0]) * 10000
                max_money = min_money

        elif u"万" in money and u"年" in money:
            pattern = re.compile(u"[-万年/]")
            result = pattern.split(money)

            if len(result) >= 2:
                min_money = float(result[0]) * 10000 / 12
                max_money = float(result[1]) * 10000 / 12
            else:
                min_money = float(result[0]) * 10000 / 12
                max_money = min_money
        elif u"千" in money and u"月" in money:
            pattern = re.compile(u"[-千月/]")
            result = pattern.split(money)
            if len(result) >= 2:
                min_money = float(result[0]) * 1000
                max_money = float(result[1]) * 1000
            else:
                min_money = float(result[0]) * 1000
                max_money = min_money
        elif u"千" in money and u"年" in money:
            pattern = re.compile(u"[-千年/]")
            result = pattern.split(money)
            if len(result) >= 2:
                min_money = float(result[0]) * 10000 / 12
                max_money = float(result[1]) * 10000 / 12
            else:
                min_money = float(result[0]) * 10000 / 12
                max_money = min_money
        elif u"元" in money and u"小时" in money:
            pattern = re.compile(u"[-元/]")
            result = pattern.split(money)
            if len(result) >= 1:
                min_money = float(result[0])
                max_money = min_money
            else:
                min_money = "面议"
                max_money = "面议"
        elif u"元" in money and u"天" in money:
            pattern = re.compile(u"[-元/]")
            result = pattern.split(money)
            if len(result) >= 1:
                min_money = float(result[0])
                max_money = min_money
            else:
                min_money = "面议"
                max_money = "面议"
        else:
            min_money = "面议"
            max_money = "面议"

        return {
            "min_money": min_money,
            "max_money": max_money,
            # 结算类型: 月/天/年...
        }


        # windows 下excel 默认是unicode
